//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_menu\fn_initUI.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_text","_disp","_ctrl","_cnt","_title","_fnc_access","_fnc_exec","_icon","_idx","_icon","_priority","_i","_added","_temp"];
disableSerialization;

_disp = _this select 0;
uiNameSpace setVariable [QGVAR(display_ui),_disp];

_cnt = 0;
_text = "";
_icon = "";
_idx = 91001;

_ctrl = _disp displayCtrl 91653;
_ctrl ctrlShow false;
_ctrl = _disp displayCtrl 91654;
_ctrl ctrlShow false;
_ctrl = _disp displayCtrl 91655;
_ctrl ctrlShow false;
GVAR(accessible_items) = [];
GVAR(number_items)  = 0;

if (isNil QGVAR(items_offset)) then { GVAR(items_offset) = 0; };
if (isNil QGVAR(SelectedItem)) then { GVAR(SelectedItem) = 0; };

{
	if (!isNil "_x" && {typeName _x == typeName []} && {count _x >= 2}) then {
		_current = _x;
		_title = GETVAL(_current,0,nil);
		_priority = 0;
		if (!isNil "_title" && {typeName _title != typeName ""}) then {
			_title = call _title;
		};
		if (count _current <= 3) then {
			_fnc_access = {true};
			_fnc_exec = GETVAL(_current,1,nil);
			_icon = GETVAL(_current,2,GVAR(DefaultIcon));
			_priority = GETVAL(_current,3,0);
		} else {
			_fnc_access = GETVAL(_current,1,{true});
			_fnc_exec = GETVAL(_current,2,nil);
			_icon = GETVAL(_current,3,GVAR(DefaultIcon));
			_priority = GETVAL(_current,4,0);
		};
		if (_icon == "") then { _icon = GVAR(DefaultIcon) };
		if (!isnil "_title" && {!isnil "_fnc_exec"} && {0 call _fnc_access}) then {
			_current = [_title,_fnc_access,_fnc_exec,_icon,_priority];
			GVAR(accessible_items) set [count GVAR(accessible_items), _current];
			GVAR(number_items) = GVAR(number_items) + 1;
		};
	};
} foreach GVAR(available_items);

TRACE_1("Available Items",ARR_4(count GVAR(available_items), count GVAR(accessible_items), GVAR(items_offset), GVAR(items_in_menu)));

_priority = [];
{
	_current = _x;
	_added = false;
	if (count _priority > 0) then {
		for "_i" from 0 to ((count _priority) - 1) step 1 do {
			TRACE_1("Compare",ARR_6((count _priority),_i,((_priority select _i) select 4),(_current select 4),((_priority select _i) select 0),(_current select 0)));
			if (((_priority select _i) select 4) < (_current select 4) || _added) then {
				_temp = (_priority select _i);
				_priority set [_i, _current];
				_current = _temp;
				_added = true;
			};
		};
	};
	_priority set [count _priority, _current];
} foreach GVAR(accessible_items);
GVAR(accessible_items) = _priority;

TRACE_1("Items Sorted!",ARR_2(count GVAR(available_items), count GVAR(accessible_items)));

for "_i" from GVAR(items_offset) to (GVAR(items_offset) + GVAR(items_in_menu) - 1) step 1 do {
	if (_i < (count GVAR(accessible_items))) then {
		_idx = 91001 + (_i - GVAR(items_offset));
		_current = GVAR(accessible_items) select _i;
		_title = _current select 0;
		_icon = GETVAL(_current,3,GVAR(DefaultIcon));
		// 250, 249, 243 - 0.9804, 0.9765, 0.9529
		// 0.0431, 0.0431, 0.0431
		// 196, 130, 20 - 0.7686, 0.5098, 0.0784
		_ctrl = _disp displayCtrl _idx;
		_ctrl ctrlSetText _title;
		_ctrl = _disp displayCtrl (_idx + 100);
		_ctrl ctrlSetText _icon;
	} else {
		
	};
};

if (GVAR(SelectedItem) >=  (count GVAR(accessible_items))) then { GVAR(SelectedItem) = (count GVAR(accessible_items)) - 1; };

if (GVAR(SelectedItem) >= 0 && GVAR(SelectedItem) < (count GVAR(accessible_items))) then {
	_current = GVAR(accessible_items) select GVAR(SelectedItem);
	_current_item = (GVAR(SelectedItem) - GVAR(items_offset));
	_title = _current select 0;
	_icon = GETVAL(_current,3,GVAR(DefaultIcon));
	_idx = 91001 + _current_item;
	_ctrl = _disp displayCtrl _idx;
	_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
	_ctrl = _disp displayCtrl (_idx + 200);
	_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
	
	if (GVAR(CONFIG_SHOW_SELECTED_OPTION) == 0) then {
		if (GVAR(CONFIG_ICON_AND_TEXT) ==0) then {
			_ctrl = _disp displayCtrl 91653;
			_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
			_ctrl ctrlSetStructuredText parseText format[GVAR(SelectedFormat),_title,_icon];
			_ctrl ctrlShow true;
		} else {
			_ctrl = _disp displayCtrl 91654;
			_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
			_ctrl ctrlShow true;
			_ctrl = _disp displayCtrl 91655;
			_ctrl ctrlSetText _icon;
			_ctrl ctrlShow true;
		};
	};
};
TRACE("End");
TMFPROFILERSTOP;
